<template>
  <div>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" :destroy-on-close="true" width="40%">
      <el-form label-width="80px" ref="form" :model="formData" :rules="rules" size="small">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="formData.userName" auto-complete="off" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="formData.password" auto-complete="off" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select v-model="formData.roleId" filterable placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="item in roleList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="formData.realName" auto-complete="off" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="formData.sex">
            <el-radio-button label="1">男</el-radio-button>
            <el-radio-button label="0">女</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="formData.mobile" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" :loading="loading" @click="save()">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { getUser, saveUser } from '@/api/sys/user'
import { getRoleSelectList } from '@/api/sys/role'

export default {
  name: 'userForm',
  data() {
    return {
      loading: false,
      title: '添加用户',
      dialogVisible: false,
      // 编辑与添加
      formData: {
        id: '',
        userName: '',
        password: '',
        realName: '',
        sex: '1',
        mobile: '',
        email: '',
        roleId: ''
      },
      roleList: [],
      // rules表单验证
      rules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请选择角色', trigger: 'blur' }
        ],
        realName: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        nickName: [
          { required: true, message: '请输入昵称', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^1(3\d|47|5((?!4)\d)|7(0|1|[6-8])|8\d)\d{8,8}$/,
            required: true,
            message: '请输入正确的手机号',
            trigger: 'blur'
          }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          {
            pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
            required: true,
            message: '请输入正确的邮箱',
            trigger: 'blur'
          }
        ]
      }
    }
  },

  created() {
    this.getRoleList()
  },

  methods: {
    getRoleList() {
      getRoleSelectList().then(res=> {
        if (res.status === 200) {
          this.roleList = res.data
        }
      })
    },
    create() {
      this.title = '添加用户'
      this.dialogVisible = true
      this.formData = {
          id: '',
          userName: '',
          password: '',
          realName: '',
          sex: '1',
          mobile: '',
          email: '',
          roleId : ''
      }
    },
    update(id) {
      getUser(id).then(res => {
          if (res.status === 200) {
              this.title = '编辑用户'
              this.dialogVisible = true
              this.formData = res.data
          }
      })
    },
    save() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // 请求方法
          this.loading = true
          saveUser(this.formData).then(res => {
              if (res.status === 200) {
                this.dialogVisible = false
                this.loading = false
                this.$emit('saveAfter')
                this.$message({
                  type: 'success',
                  message: '保存成功！'
                })
              }
            })
            .catch(err => {
              this.dialogVisible = false
              this.loading = false
              this.$message.error('保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 